define(function (require, exports, module) {
    var angular = require('angular');
    require("pagination");
    require("angular-ui");
    require('/public/js/myservice/contract/contract-common');
    var myMath = require('/public/js/myservice/common/my-math');
    var util = require("util");
    var api_prefix = G_CONFIG["api_prefix"]; //api前缀

    angular.module('ContractIncome', ['ContractCommonService'])
        .filter('sumAmount', function () {
            return function (incomes,fieldname) {
                var sumamount = 0;
                $.each(incomes, function (i, d) {
                    sumamount = myMath.add(sumamount, d.amount);
                });
                return sumamount;
            }
        })
        .filter('sumHaveConfirmAmount', function () {
            return function (incomes) {
                var sumamount = 0;
                $.each(incomes, function (i, d) {
                    sumamount = myMath.add(sumamount, parseFloat(d.total_confirm_amount));
                });
                return sumamount;
            }
        })
        .filter('sumWillConfirmAmount', function () {
            return function (incomes) {
                var sumamount = 0;
                $.each(incomes, function (i, d) {
                    sumamount = myMath.add(sumamount, parseFloat(d.wait_confirm_amount));
                });
                return sumamount;
            }
        })
        .filter('sumHaveConfirmBadAmount', function () {
            return function (incomes) {
                var sumamount = 0;
                $.each(incomes, function (i, d) {
                    sumamount = myMath.add(sumamount, parseFloat(d.have_confirm_bad_amount));
                });
                return sumamount;
            }
        })
        .filter('sumNoConfirmBadAmount', function () {
            return function (incomes) {
                var sumamount = 0;
                $.each(incomes, function (i, d) {
                    sumamount = myMath.add(sumamount, parseFloat(d.no_confirm_bad_amount));
                });
                return sumamount;
            }
        })
        .filter('sumKingdieConfirmBadAmount', function () {
            return function (incomes) {
                var sumamount = 0;
                $.each(incomes, function (i, d) {
                    sumamount = myMath.add(sumamount, parseFloat(d.kingdie_verif_bad_amount));
                });
                return sumamount;
            }
        })

        .service('contractIncomeService',['$rootScope', 'contractCommonService', function ($rootScope, contractCommonService) {
            var service = {
                onIncomeEditShow: function ($data,$dialog,$dialogScope) {
                    var oid = $data.oid;
                    $dialogScope.isEdit = $data.isEdit;
                    $dialogScope.revenueinfo = null;
                    contractCommonService.getContractRevenueById(oid, function (data) {
                        $dialogScope.revenueinfo = data;
                        $dialogScope.$applyAsync();
                    });
                },
                editIncomeSave: function($data,$dialog,$dialogScope,record){
                    var post_data = {
                        revenueId: $dialogScope.revenueinfo.revenue_id,
                        noconfirmbadamount: $dialogScope.revenueinfo.no_confirm_bad_amount,
                        haveconfirmbadamount: $dialogScope.revenueinfo.have_confirm_bad_amount,
                        kingdieverifbadamount: $dialogScope.revenueinfo.kingdie_verif_bad_amount
                    };
                    var url = api_prefix + "/contract/save-revenue-info";
                    util.myAjax({
                        url: url,
                        type: "POST",
                        context: this,
                        data: post_data,
                        success: function (json) {
                            if (json.retCode === "0") {
                                record.no_confirm_bad_amount = $dialogScope.revenueinfo.no_confirm_bad_amount;
                                util.alert('保存成功');
                            } else {
                                util.alert("保存失败：" + json.errMsg);
                            }
                        }
                    });
                },
                onIncomeCarryoverEditShow: function ($data,$dialog,$dialogScope) {
                    $dialogScope.carryoverinfo = $data;
                    $dialogScope.carryoverinfo.change_after_this_period_confirm_total_amount = $data.this_period_confirm_total_amount;
                    $dialogScope.$applyAsync();
                    $dialogScope.$watch('carryoverinfo.change_amount', function (idx,item) {
                        $dialogScope.carryoverinfo.change_after_this_period_confirm_total_amount = $dialogScope.getFloatData($dialogScope.carryoverinfo.last_period_total_confirm_amount) + $dialogScope.getFloatData($dialogScope.carryoverinfo.change_amount) + $dialogScope.getFloatData($dialogScope.carryoverinfo.this_period_confirm_amount);
                    });
                    $dialogScope.$watch('carryoverinfo.this_period_confirm_amount', function (idx,item) {
                        $dialogScope.carryoverinfo.change_after_this_period_confirm_total_amount = $dialogScope.getFloatData($dialogScope.carryoverinfo.last_period_total_confirm_amount) + $dialogScope.getFloatData($dialogScope.carryoverinfo.change_amount) + $dialogScope.getFloatData($dialogScope.carryoverinfo.this_period_confirm_amount);
                    });
                    $dialogScope.getFloatData = function (itemvalue) {
                        if(itemvalue == null || itemvalue == ''){
                            return 0;
                        }else{
                            return parseFloat(itemvalue);
                        }
                    }
                },
                editIncomeCarryoverSave: function ($data,$dialog,$dialogScope,record) {
                    var post_data = {
                        carryoverId: $dialogScope.carryoverinfo.carryover_id,
                        revenueId: $dialogScope.carryoverinfo.revenue_id,
                        changeamount: $dialogScope.carryoverinfo.change_amount,
                        changebeforethisperiodconfirmtotalamount: $dialogScope.carryoverinfo.this_period_confirm_total_amount,
                        thisperiodconfirmtotalamount: $dialogScope.carryoverinfo.change_after_this_period_confirm_total_amount,
                        thisperiodconfirmamount: $dialogScope.carryoverinfo.this_period_confirm_amount,
                    };
                    var url = api_prefix + "/contract/save-revenue-carryover-info";
                    util.myAjax({
                        url: url,
                        type: "POST",
                        context: this,
                        data: post_data,
                        success: function (json) {
                            if (json.retCode === "0") {
                                record.this_period_confirm_amount = $dialogScope.carryoverinfo.this_period_confirm_amount;
                                record.this_period_confirm_total_amount = $dialogScope.carryoverinfo.change_after_this_period_confirm_total_amount;
                                record.this_period_confirm_amount = $dialogScope.carryoverinfo.this_period_confirm_amount;
                                util.alert('保存成功');
                            } else {
                                util.alert("保存失败：" + json.errMsg);
                            }
                        }
                    });
                }

            };
            return service;
        }])

        .controller('contractIncomeCtrl', ['$scope', '$routeParams','contractIncomeService', 'contractCommonService', function ($scope, $routeParams,contractIncomeService, contractCommonService) {
            $scope.contractIncomeService = contractIncomeService;
            var contractId = $routeParams.id;

            // 合同签署公司下拉框处理
            $scope.ddlConfigCompany = {
                allowClear: false,
                data: [{id: 1, text: '明源云链'}],
                placeholder: '账套公司'
            };

            //获取合同信息
            contractCommonService.getContract(contractId, function (data) {
                if(data){
                    //获取公司下拉信息
                    contractCommonService.actionGetContractAccountCompanyList(data.contract.contract_property_id, function (data2) {
                        var ddlData = [];
                        $.each(data2, function (i, d) {
                            ddlData.push({id: d.account_company_id, text: d.account_company_name});
                        });
                        $scope.ddlConfigCompany.data = ddlData;
                        $scope.contractBaseInfo = data.contract;
                        if(!$scope.contractBaseInfo.account_company_id){
                            ddlData[0] && ($scope.contractBaseInfo.account_company_id = ddlData[0].id);
                        }
                        $scope.$applyAsync();

                    });
                }
            });

            //收入信息
            $scope.incomes = [];
            $scope.revenueInfo = {};

            $scope.refreshRevenueList = function () {
                //获取收入
                contractCommonService.getContractRevenue(contractId, function (data) {
                    $scope.revenueInfo = data;
                    $scope.incomes = data.revenueList;
                    $scope.carryovers = data.carryoverList;
                    $scope.$applyAsync();
                });
            };

            $scope.refreshRevenueList();

            $scope.save = function () {
                
                //校验
                /*
                */
                var sumIncomeAmount = 0;
                $.each($scope.incomes, function (i, d) {
                    sumIncomeAmount = myMath.add(sumIncomeAmount, parseFloat(d.amount));
                });
                //if(myMath.sub($scope.contractBaseInfo.income_amount, sumIncomeAmount) < 0){
                //    util.alert('已确定收入不能大于收入金额，请调整收入记录！');
                //    return false;
                //}
                
                /**
                 * 保存收入信息
                 */

                var baseInfo = $scope.contractBaseInfo;
                var incomesInfo = $scope.incomes;

                var post_data = {
                    contractId: contractId,
                    revenueList: incomesInfo,
                    accountCompanyId: baseInfo.account_company_id
                };
                var url = api_prefix + "/contract/submit-revenue";
                util.myAjax({
                    url: url,
                    type: "POST",
                    context: this,
                    data: post_data,
                    success: function (json) {
                        if (json.retCode === "0") {
                            $scope.incomes = json.data.revenueList;
                            $.each($scope.incomes, function(i, d){
                                if(d.__type=='add'){
                                    d.__type='edit';
                                }
                            });
                            $scope.$applyAsync();
                            util.alert('保存成功');
                        } else {
                            util.alert("保存失败：" + json.errMsg);
                        }
                    }
                });
            }
        }]);
});